<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/12
 * Time: 20:29
 */

namespace frontend\controllers;

use Yii;
use yii\rest\Controller;

class BaseController extends Controller{

    /** 获取post数据 */
    public $postData = null;

    public function init()
    {
        $this->postData = Yii::$app->request->post();
    }

    /**
     * 错误返回信息 type = 0
     * @param 错误信息
     * @return array
     */
    public function responseError($code = 0, $msg)
    {
        $response = array(
            'type' => $code,
            'msg' => $msg
        );
        return $response;
    }

    /**
     * 失败返回信息 type = 1
     * @param 失败返回信息
     * @return array
     */
    public function responseFail($msg)
    {
        if (empty($result)) {
            $result = "Internal server error.";
        }
        $response = array(
            'type' => 1,
            'result' => false,
            'msg' => $msg
        );
        return $response;
    }
    /**
     * 正常返回格式化 type = 2
     * @param $result  返回结果
     * @return array
     */
    public function responseFormat($result)
    {
        if (is_array($result)) {
            $response = array(
                'type' => 2,
                'result' => $result,
            );
        } else {
            $response = array(
                'type' => 2,
                'result' => json_decode($result),
            );
        }
        return $response;
    }
}
